<?php
/*
<Controller Information> 
Author: Naseer Ahmad
Date: 17-10-2011
Last Updated on:
*/

class areaManagerController extends appController
{
	function index()
	{
		
		/////////// get All regions
		$regions = $this -> areaManager -> _getArrayData("regions", "*");
		$this -> set("regions", $regions);
		
		////////// set default filter
		if(isset($this->data['filterBy'])) $filterBy=$this->data['filterBy']; else $filterBy=0;
		$this->set("filterBy", $filterBy);
		
		
		////////// get all cities in this area
		if($filterBy!=0) $criteria = "regionID=".$filterBy; else $criteria="1=1";
		$arrAreas = $this -> areaManager -> _getArrayData("city", "*", $criteria);
		$this->set("arrAreas", $arrAreas);
		
		////////// check for edit
		if(isset($this -> data['cityId'])){
			$this->set("action","Update");
			$editArea = $this -> areaManager -> _getRowData("city", "*", "cityID=".$this -> data['cityId']);
			$this->set("editArea",$editArea);
		}
		else
			$this->set("action","Save");
		
		
		$this -> set("cmd", $this->data['cmd']);
		
		$this -> render('Area Manager');
	}

	function Save(){
		//print "<pre>";
		//print_r($this->data);
		//print "</pre>";
		
		v("City Name", $this->data["cityName"]);
		
		if(!isset($_SESSION[v])){
			$fv= array("regionID"	=> $this->data['regionId'],
						"cityName"	=> $this->data['cityName']);
						
			$rs = $this -> areaManager -> _insertTable("city", $fv);
			if($rs) $cmd="Area Successfully Saved!"; else $cmd = $this->areaManager->getError();
		}
		else
			$cmd = $_SESSION[v];
		
		$rdArray = array("page"	=> "areaManager",
						"cmd"	=> $cmd,
						"filterBy"	=> $this->data['filterBy']);
		
		$this->redirect($rdArray);
	}

	function Update(){ 
		//print "<pre>";
		//print_r($this->data);
		//print "</pre>";
		
		v("City Name", $this->data["cityName"]);
		
		if(!isset($_SESSION[v])){
			$fv= array("regionID"	=> $this->data['regionId'],
						"cityName"	=> $this->data['cityName']);
			$where = array("cityID"	=> $this->data['cityId']);
						
			$rs = $this -> areaManager -> _updateTable("city", $fv, $where);
			if($rs) $cmd="Area Successfully Updated!"; else $cmd = $this->areaManager->getError();
		}
		else
			$cmd = $_SESSION[v];
		
		$rdArray = array("page"	=> "areaManager",
						"cmd"	=> $cmd,
						"filterBy"	=> $this->data['regionId']);
		$this->redirect($rdArray);
	}

	function delArea(){
		$where = array("cityID"	=> $this->data['cityId']);
		$rs = $this -> areaManager -> _XTable("city", $where);
		if($rs) $cmd="Area Successfully Deleted!"; else $cmd = $this->areaManager->getError();
		$rdArray = array("page"	=> "areaManager",
						"cmd"	=> $cmd,
						"filterBy"	=> $this->data['filterBy']);
		$this->redirect($rdArray);
	}
	
}
?>